package com.hxkj.controller.article;

import com.hxkj.domain.po.Resources;
import com.hxkj.domain.po.Templet;
import com.hxkj.domain.po.TemplteTree;
import com.hxkj.service.FileService;
import com.hxkj.service.TemplteService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 模板控制层
 * <p>
 * Created by yaosy on 2018年6月7日
 */
@RestController
@RequestMapping("/templet")
public class TemplteController {

    @Autowired
    private TemplteService templteService;

    /**
     * 新增模板
     *
     * @param templet
     * @throws Exception
     */
    @RequestMapping(value = "/saveContent")
    public Callable<Templet> saveContent(@ModelAttribute Templet templet) throws Exception {
        if(templet.getIsDirectory()==1 || templet.getIsDirectory()== 2){
            return ()-> templteService.saveContent(templet);
        }else {
            return ()-> templteService.saveFolder(templet);
        }
    }

    /**
     * 新增目录
     *
     * @param templet
     * @throws Exception
     */
    @RequestMapping(value = "/saveFolder")
    public Callable<Templet> saveFolder(@ModelAttribute Templet templet) throws Exception {
        return ()-> templteService.saveFolder(templet);
    }

    /**
     * 获取模板功能树
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getTempletTree")
    public Callable<List<TemplteTree>> getTempletTree(@ModelAttribute Templet templet) throws Exception {
        return () ->templteService.getTempletTree(templet);
    }


    /**
     * 删除模板
     *
     * @param templet
     * @throws Exception
     */
    @RequestMapping(value = "/remove")
    public Callable<ResultMessageStruct> remove(@ModelAttribute Templet templet) throws Exception{
        return () -> templteService.remove(templet);
    }

    /**
     * 获取单条模板数据
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/get")
    public Callable<ResultDataStruct<Templet>> get(@ModelAttribute Templet templet) throws Exception {
        return () ->templteService.get(templet);
    }

    /**
     * 批量模板上传
     *
     * @param file 图片信息   第二个参数为自定义一个 存放文件的目录名称
     * @throws Exception
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Callable<List<String>> upload(@RequestParam(value = "file", required = false) MultipartFile[] file) throws Exception {
        //return () -> templteService.upload(file);
        return null;
    }

    /**
     * 获取模板功能树
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getTree")
    public Callable<List<TemplteTree>> getTree(@ModelAttribute Templet templet) throws Exception {
        return () ->templteService.getTree(templet);
    }

    /**
     * 保存单条模板文件
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/modify")
    public Callable<ResultMessageStruct> modify(@ModelAttribute Templet templet) throws Exception {
        return () ->templteService.modify(templet);
    }

    /***
     * 上传文件后保存到数据库当中
     *
     * @param templet
     * @throws Exception
     * ***/
    @RequestMapping(value = "/saveUploadContent")
    public Callable<Templet> saveUploadContent(@ModelAttribute Templet templet) throws Exception {
        return ()-> templteService.saveUploadContent(templet);
    }

    /***
     * 上传文件后保存到数据库当中
     *
     * @param templet
     * @throws Exception
     * ***/
    @RequestMapping(value = "/switchTemplte")
    public void switchTemplte(@ModelAttribute Templet templet) throws Exception {
         templteService.switchTemplte(templet);
    }


   /**
   * @author Mr.zheng
   * @Description download 根据站点下载模板文件
   * @Date 9:21 2018/8/17
   * @Param [request, response, templet]
   * @return void
   **/
    @RequestMapping(value = "/download")
    public void downloadTemplet(HttpServletRequest request, HttpServletResponse response , @ModelAttribute Templet templet) throws Exception {
        templteService.downloadTemplet(request , response, templet);
    }

    /**
     * 可视化模板接口
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/visuaTempletTree")
    public Callable<List<TemplteTree>> visuaTempletTree(@ModelAttribute Templet templet) throws Exception{
        return ()-> templteService.visuaTempletTree(templet);
    }
}
